<template>
  <div class="side-navigation">
    <ul>
      <li>
        <img
          src="https://www.guohuihr.cn/statics/guohuijituan/images/2339.png"
        />
        <p>服务热线</p>
      </li>
      <li>
        <img
          src="https://www.guohuihr.cn/statics/guohuijituan/images/2340.png"
        />
        <p>联系电话</p>
      </li>
      <li>
        <img
          src="https://www.guohuihr.cn/statics/guohuijituan/images/23401.png"
        />
        <p>联系微信</p>
      </li>
      <li>
        <img
          src="https://www.guohuihr.cn/statics/guohuijituan/images/23401.png"
        />
        <p>联系微信</p>
      </li>
      <li v-show="btnFlag" @click="backTop">
        <img
          src="https://www.guohuihr.cn/statics/guohuijituan/images/23403.png"
        />
        <p>回到顶部</p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "SideNavigation",
  data() {
    return {
      btnFlag: false,
    };
  },
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollToTop);
  },
  methods: {
    backTop() {
      const that = this;
      let timer = setInterval(() => {
        let ispeed = Math.floor(-that.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          that.scrollTop + ispeed;
        if (that.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 16);
    },

    // 为了计算距离顶部的高度，当高度大于60显示回顶部图标，小于60则隐藏
    scrollToTop() {
      const that = this;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      that.scrollTop = scrollTop;
      if (that.scrollTop > 60) {
        that.btnFlag = true;
      } else {
        D;
        that.btnFlag = false;
      }
    },
  },
};
</script>


<style lang="less" scoped>
.side-navigation {
  position: fixed;
  right: 20px;
  top: 30%;
  z-index: 999;
  height: 300px;
  border: 1px solid rgba(0, 0, 0, 0);
  ul li {
    width: 60px;
    height: 60px;
    background-color: #fffafa;
    border-top: 1px #ccc solid;
    border-bottom: 1px #ccc solid;
    list-style: none;
    text-align: center;
    display: flex;
    flex-direction: column;
    p {
      color: #000;
      font-size: 12px;
    }
    img {
      width: 22px;
      margin: 10px auto;
      height: 19px;
    }
  }
  ul li:nth-last-child(1) {
    margin-top: 30px;
  }
}
</style>